package com.hndzyu.yd.my;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.gyf.barlibrary.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.hndzyu.yd.R;
import com.hndzyu.yd.bean.MyMsg;
import com.hndzyu.yd.bean.UserBean;
import com.hndzyu.yd.url.Path;
import com.hndzyu.yd.utils.OkHttpDownloadJsonUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MyMessageActivity extends AppCompatActivity {

    @BindView(R.id.top_back)
    ImageButton topBack;
    @BindView(R.id.top_title)
    TextView topTitle;
    @BindView(R.id.recycler)
    RecyclerView recycler;
    @BindView(R.id.srl)
    SmartRefreshLayout srl;
    private MyMsgAdapter mAdapter;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_message);
        ButterKnife.bind(this);
        ImmersionBar.with(this)
                .fitsSystemWindows(true)  //使用该属性,必须指定状态栏颜色
                .statusBarColor(R.color.white)
                .statusBarDarkFont(true, 0.2f)
                .init();
        topTitle.setText("我的消息");
        setListener();
        initData();
    }

    private void setListener() {
        topBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        srl.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                msglist(UserBean.uid, page);
            }
        });
        srl.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page ++;
                msglist(UserBean.uid,page);
            }
        });
    }
    private void initData(){
        recycler.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new MyMsgAdapter(this);
        recycler.setAdapter(mAdapter);
        msglist(UserBean.uid, page);
    }
    private void msglist(int uid, int cPage){
        OkHttpDownloadJsonUtil.downloadJson(MyMessageActivity.this, Path.myMessage + "?uid=" + uid + "&page=" + cPage, new OkHttpDownloadJsonUtil.onOkHttpDownloadListener() {

            @Override
            public void onsendJson(String json) {
                srl.finishRefresh();
                srl.finishLoadMore();
                if (!"1".equals(json) && !"2".equals(json)){
                    Gson gson = new Gson();
                    MyMsg tiBean = gson.fromJson(json, MyMsg.class);
                    if (tiBean.getMessage().getData().size()>0){
                        mAdapter.updateData(page,tiBean.getMessage().getData());
                    } else {
                        if (page == 1){
                            Toast.makeText(MyMessageActivity.this, "暂无消息", Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(MyMessageActivity.this, "没有更多消息了", Toast.LENGTH_SHORT).show();
                        }
                    }
                }else {
                    Toast.makeText(MyMessageActivity.this, "网络异常", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }
}
